#include <iostream>
#include <string>
#include <unordered_map>
#include<deque>
using namespace std;

deque<pair<string, int>> f;
deque<pair<string, int>> m;
int a[50];
int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int j;
        string s;
        cin >> j >> s;
        if (j == 0)
            f.push_back({s, j});
        else
            m.push_back({s, j});
        a[i] = j;
    }
    
    // reverse(m.begin(), m.end());
    for (int i = 0; i < n; i++)
    {
        int b = a[i];
        if (b == 1)
        {
            auto x = m.front(); m.pop_front();
            auto y = f.back(); f.pop_back();
            cout << x.first << " " << y.first << endl;
        }
        else 
        {
            auto x = f.front(); f.pop_front();
            auto y = m.back(); m.pop_back();
            cout << x.first << " " << y.first << endl;
        }
        if (!m.size()) break;
    }
    return 0;
}